package gov.kqqzyyy.monkey.ydhl.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.widget.FrameLayout;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import adapter.BuzRecyclerAdapter;
import butterknife.BindView;
import butterknife.ButterKnife;
import gov.kqqzyyy.monkey.ydhl.R;
import objects.InPatient;

public class PatientDetailActivity extends NavigationBaseActivity {

    @BindView(R.id.container)
    FrameLayout container;

    private RecyclerView recyclerView;

    private BuzRecyclerAdapter mRecyclerAdapter;

    private Handler myHandler = new Handler();
    private Runnable mLoadingRunnable = this::loadView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
    }

    private void loadView() {
        container.removeAllViews();
        LayoutInflater.from(this).inflate(R.layout.activity_patient_detail, container);
        onBindUI();

        mRecyclerAdapter = new BuzRecyclerAdapter(this);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3);
        recyclerView.setLayoutManager(gridLayoutManager);
        recyclerView.setAdapter(mRecyclerAdapter);

        mRecyclerAdapter.setOnItemClickListener((view, data) -> {
            Intent intent;
            switch (data) {
                case "计划复核":
                    intent = new Intent(PatientDetailActivity.this, PlanCheckActivity.class);
                    startActivity(intent);
                    break;
                case "计划执行":
                    intent = new Intent(PatientDetailActivity.this, PlanExecuteActivity.class);
                    startActivity(intent);
                    break;
                case "费用查询":
                    intent = new Intent(PatientDetailActivity.this, CostActivity.class);
                    startActivity(intent);
                    break;
                case "检验报告":
                    intent = new Intent(PatientDetailActivity.this, ReportJYActivity.class);
                    startActivity(intent);
                    break;
                case "检查报告":
                    intent = new Intent(PatientDetailActivity.this, ReportJCActivity.class);
                    startActivity(intent);
                    break;
                case "健康宣教":
                    intent = new Intent(PatientDetailActivity.this, HeathEducationActivity.class);
                    startActivity(intent);
                    break;
                default:
                    Toast.makeText(PatientDetailActivity.this, data + " clicked", Toast.LENGTH_SHORT).show();
                    break;
            }
        });

        InPatient inPatient = getMyApplication().getChoosePatient();
        if (inPatient != null) {
            setActionBarText(inPatient.getBRXM() + " " + inPatient.getBRXB());
            loadBtns();
        } else {
            Toast.makeText(this, "获取病人数据失败，请重新登陆！", Toast.LENGTH_SHORT).show();
//            startActivity(new Intent(this, UserLoginActivity.class));
        }

    }

    private void onBindUI() {
        recyclerView = findViewById(R.id.recyler_button);
    }

    @Override
    protected void onResume() {
        super.onResume();
        getWindow().getDecorView().post(() -> myHandler.post(mLoadingRunnable));
    }

    //加载按钮
    private void loadBtns() {
        List<String> names = new ArrayList<>();
        List<Integer> images = new ArrayList<>();
        String[] btn_values = {"医嘱明细", "计划复核", "计划执行", "费用查询", "检验报告", "检查报告", "健康宣教", "按钮7", "按钮8", "按钮9"};
        for (int i = 0; i < btn_values.length; i++) {
            int imageID;
            String picname;
            switch (btn_values[i]) {
                case "医嘱明细":
                    picname = "ic_doctor";
                    break;
                case "计划复核":
                    picname = "ic_report";
                    break;
                case "计划执行":
                    picname = "ic_syringe";
                    break;
                case "费用查询":
                    picname = "ic_dollar";
                    break;
                case "检验报告":
                    picname = "ic_examination";
                    break;
                case "检查报告":
                    picname = "ic_xray";
                    break;
                case "健康宣教":
                    picname = "ic_info";
                    break;
                default:
                    picname = String.format(Locale.CHINA, "ic_num%02d", i);
                    break;
            }
            imageID = getResources().getIdentifier(picname, "drawable", getApplicationContext().getPackageName());
            images.add(imageID);
            names.add(btn_values[i]);
        }
        if (names.size() > 0) {
            mRecyclerAdapter.setButtonText(names);
            mRecyclerAdapter.setButtonImage(images);
        }
    }

}
